<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" isELIgnored="false"%>
<%@ include file="/WEB-INF/views/shared/taglib.jsp"%>

<%
	String excelId = (String)session.getAttribute("excelId");
%>
<div style="margin-top: 10px;">
	
	<div class="hot-container has-scroll">
		<div id="hot_my"></div>
	</div>
</div>
<script type="text/javascript">
	var $hot= null;
	window.key = "true";
	var empty=function(value, callback){
		setTimeout(function(){
			callback(!$.isNull(value));	
		},10);
	};
	
	function createSpreadsheetData(rowCount, colCount) {
		rowCount = typeof rowCount === 'number' ? rowCount : 100;
		colCount = typeof colCount === 'number' ? colCount : 4;
		var rows = [], i, j;
		for (i = 0; i < rowCount; i++) {
			var row = [];
			for (j = 0; j < colCount; j++) {
				row.push('');
			}
			rows.push(row);
		}
		return rows;
	}
	
	function init(){
		var hot_my = document.getElementById('hot_my');
		
	    // Initialize with options
	    $hot= new Handsontable(hot_my, {
	        startRows:10,
	        startCols:6,
	        manualColumnResize:true,
	        manualRowResize:true,
	        manualColumnMove:true,
	        manualRowMove:true,
	        columnSorting:true,
	        rowHeaders: true,
	        colHeaders: true,
	        contextMenu: true,
	        autoColumnSize: true,
	        stretchH: 'all',
	        mergeCells: true,  
	        manualColumnFreeze: true
	    });
	    
	   /*  $hot.updateSettings({
			contextMenu : {
				items : {
					"row_above" : {name : '在上面添加行'},
					"row_below" : {name : '在下面添加行'},
					"remove_row" : {name : '删除当前行'}
				}
			}
		}); */
	}
		
	
	function loadTable(){
		var excelId = "<%=excelId %>";
		var json=null;
		var url=$.getVirtualPath()+"/admin/experimentExcel/data/dictColumnJson?id="+menuId+"&excelId="+excelId;
		json=$.getJsonPost(url);
		c.log(json);
		if($.isNull(json)){
			alert("2323");	
			key="false";
			return getEmptyData();
		}
		return json;
	}
	
	function getEmptyData() {
		var hot_borders_data = createSpreadsheetData(10,6);
		return hot_borders_data;	
	}
	
	function reload(){	
		var data=loadTable();
		if(key=="true"){
			var dataArray = JSON.parse(data);
			$hot.loadData(dataArray);
			c.log(dataArray);
			$hot.render();
		}else{
			$hot.loadData(data);
			$hot.render();
			key = "true";
		}
	}
	
	function saveForm(){
		var excelId = "<%=excelId %>";
		deleteEmpty();
		var datas = $hot.getData();
		c.log(datas);
		var url=$.getVirtualPath()+"/admin/experimentExcel/data/save?id="+menuId+"&excelId="+excelId;
		c.log(url);
		$.ajax({
			url:url,
			type:'post',
			dataType:'json',
			contentType:"application/json",
			data:JSON.stringify(datas),
			success:function(result){
				$.topNotify(result.message,result.resultType);	
			}
		});
	}
	
	//删除空行
	function deleteEmpty(){
		var rows=$hot.countRows();
		for(var i=rows-1;i>=0;i--){
			if($hot.isEmptyRow(i)){
				$hot.alter('remove_row', i);
			}
		}
	}
	
</script>

	

<%@ include
	file="/WEB-INF/views/shared/base/scripts/limitless/default/handsontable.jsp"%>
	<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/ui/prism.jsp"%>